﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using EntityObject;
using ThangNM_LIB.FUNC;

namespace DataAccessObject
{
    public class ErrorDAO : DB_MSAccess_EXT
    {
        public Boolean InsertItem(ErrorEO _ErrorEO)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "Error_InsertItem";
            cmd.Parameters.Add("@sLink", OleDbType.VarChar).Value = _ErrorEO.sLink;
            cmd.Parameters.Add("@sIP", OleDbType.VarChar).Value = _ErrorEO.sIP;
            cmd.Parameters.Add("@sBrowser", OleDbType.VarChar).Value = _ErrorEO.sBrowser;
            cmd.Parameters.Add("@iCode", OleDbType.Numeric).Value = _ErrorEO.iCode;
            cmd.Parameters.Add("@iStatus", OleDbType.Numeric).Value = _ErrorEO.iStatus;
            cmd.Parameters.Add("@tTime", OleDbType.Date).Value = _ErrorEO.tTime;
            cmd.Parameters.Add("@tTimeCheck", OleDbType.Date).Value = _ErrorEO.tTimeCheck;
            ExecuteNonQuery(cmd);
            cmd.Connection.Close();
            return true;
        }

        public Boolean UpdateItem(ErrorEO _ErrorEO)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "Error_UpdateItem";
            cmd.Parameters.Add("@sLink", OleDbType.VarChar).Value = _ErrorEO.sLink;
            cmd.Parameters.Add("@sIP", OleDbType.VarChar).Value = _ErrorEO.sIP;
            cmd.Parameters.Add("@sBrowser", OleDbType.VarChar).Value = _ErrorEO.sBrowser;
            cmd.Parameters.Add("@iCode", OleDbType.Numeric).Value = _ErrorEO.iCode;
            cmd.Parameters.Add("@iStatus", OleDbType.Numeric).Value = _ErrorEO.iStatus;
            cmd.Parameters.Add("@tTime", OleDbType.Date).Value = _ErrorEO.tTime;
            cmd.Parameters.Add("@tTimeCheck", OleDbType.Date).Value = _ErrorEO.tTimeCheck;
            cmd.Parameters.Add("@PK_lErrorID", OleDbType.Numeric).Value = _ErrorEO.PK_lErrorID;
            ExecuteNonQuery(cmd);
            cmd.Connection.Close();
            return true;
        }

        public Boolean DeleteItem(string _PK_lErrorID, Int16 _iType, String _loc_old)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "Error_DeleteItem";
            cmd.Parameters.Add("@PK_lErrorID", OleDbType.Numeric).Value = _PK_lErrorID;
            cmd.Parameters.Add(new SqlParameter("@loc_old", _loc_old));
            ExecuteNonQuery(cmd);
            cmd.Connection.Close();
            return true;
        }

        public DataTable SelectList()
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "Error_SelectList";
            return GetDataTable(cmd);
        }

        public ErrorEO SelectItem(Int64 _PK_lErrorID)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "Error_SelectItem";
            cmd.Parameters.Add("@PK_lErrorID", OleDbType.Numeric).Value = _PK_lErrorID;
            return ErrorDO(GetDataTable(cmd));
        }

        public ErrorEO ErrorDO(DataTable input)
        {
            List<ErrorEO> lst = new List<ErrorEO>();
            lst = (List<ErrorEO>)ConvertEx.ConvertDataTableToListObject<ErrorEO>(input, lst.GetType());
            if (CommonsCheck.ListObject_OK(lst))
                return lst[0];
            else
                return null;
        }
    }
}